package com.gd.coreservice.controller;


import com.gd.commonutils.Constants;
import com.gd.commonutils.Result;
import com.gd.coreservice.entity.Orders;
import com.gd.coreservice.entity.UserLeave;
import com.gd.coreservice.entity.Users;
import com.gd.coreservice.service.OrdersService;
import com.gd.coreservice.service.UserLeaveService;
import com.gd.servicebase.exceptionhandler.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2024-03-23
 */
@RestController
@RequestMapping("/leave")
public class UserLeaveController {
    @Autowired
    UserLeaveService userLeaveService;
    //增加  已测试
    @PostMapping("/insertLeave")
    public Result addLeave(@RequestParam("Uid") String Uid, @RequestParam("Gid") String Gid, @RequestParam("Lmessage") String Lmessage) {
        if(userLeaveService.insertLeave(Uid,Gid,Lmessage))
        {
            return Result.success("留言成功");
        }
        else
        {
            throw new ServiceException(Constants.CODE_600, "后台异常");//自定义异常
        }

    }

    //删除  已测试
    @DeleteMapping("/deleteLeave/{Lid}&{Uid}")
    public Integer deleteLeave(@PathVariable("Lid") String Lid,@PathVariable("Uid") String Uid){
        return userLeaveService.deleteLeave(Lid,Uid);
    }

    //商品留言分页查询 查询条件搜索Gid 已测试
    @GetMapping("/getLeavePage")
    public Map<String, Object> getLeavePage(@RequestParam Integer pageNum,
                                            @RequestParam Integer pageSize,
                                            @RequestParam String Gid) {
        HashMap<String, Object> resultMap = new HashMap<>();
        Map<String, Object> result = userLeaveService.getUserLeavePageData(pageNum,pageSize,Gid);
        List<UserLeave> data = (List<UserLeave>) result.get("data");
        Long total = (Long) result.get("total");
        resultMap.put("data",data);
        resultMap.put("total",total);
        return resultMap;//返回结果Map集合：{data：List<User>集合，total：总的结果数} map集合.put添加key value
    }

    //商品留言分页查询(使用Uid查询) 已测试
    @GetMapping("/getLeavePageUid")
    public Map<String, Object> getLeavePageUid(@RequestParam Integer pageNum,
                                               @RequestParam Integer pageSize,
                                               @RequestParam String Uid) {
        HashMap<String, Object> resultMap = new HashMap<>();
        Map<String, Object> result = userLeaveService.getUserLeavePageDataUid(pageNum,pageSize,Uid);
        List<UserLeave> data = (List<UserLeave>) result.get("data");
        Long total = (Long) result.get("total");
        resultMap.put("data",data);
        resultMap.put("total",total);
        return resultMap;//返回结果Map集合：{data：List<User>集合，total：总的结果数} map集合.put添加key value
    }

}

